package com.woorigil.social;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class HtmlUtils {
	public static String escapeHtml(String str) {
		if (StringUtils.isBlank(str)) {
			return str;
		}
		
		return str.replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;").
			replaceAll("\"", "&quot;").replaceAll("'", "&#39;");
	}
	
	public static String br2nl(String html) {
	    Document document = Jsoup.parse(html);
	    document.select("br").append("\\n");
	    document.select("p").prepend("\\n\\n");
	    return document.text().replaceAll("\\\\n", "\n");
	}

	public static String unescapeHtml(String str) {
		if (StringUtils.isBlank(str)) {
			return str;
		}
		
		return str.replaceAll("&#39;", "'").replaceAll("&quot;", "\"").replaceAll("&lt;", "<").
			replaceAll("&gt;", ">").replaceAll("&amp;", "&");
	}
	
	/**
	 * html tag 제거하는 함수
	 * 
	 * @param html
	 * @return
	 */
	public static String removeTag(String html) {
		if (StringUtils.isBlank(html)) {
			return html;
		}
		
		return html.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", "");
	}	
	
	public static String removeImgTag(String html) {
		if (StringUtils.isBlank(html)) {
			return html;
		}
		
		html = html.replaceAll("<img .*?>", ""); 
		html = html.replaceAll("<IMG .*?>", "");
		html = html.replaceAll("\n<CENTER>", "");
		html = html.replaceAll("<CENTER>", "");
		html = html.replaceAll("</CENTER>", "");
		html = html.replaceAll("\n<center>", "");
		html = html.replaceAll("<center>", "");
		html = html.replaceAll("</center>", "");
		html = html.replaceAll("\\r\\n", "");
		html = StringUtils.replace(html, "\\r\\n", "");
		return html;
	}
	
	/**
	 * ui에서 alert message를 뿌리기 위해 ', " 앞에 \를 붙여준다.
	 * 
	 * @param str
	 * @return
	 */
	public static String escapeQuotation(String str) {
		if (StringUtils.isBlank(str)) {
			return str;
		}
		return str.replaceAll("\'", "\\\\'").replaceAll("\"", "\\\\\"");
	}
	
	/*
	public static String removeXSS(String input) {
		String patternStr =
			"(<[/]*(?:applet|script)[^>]*>[\\s]*(?:<!--)?)" 
			+ "|(-->[\\s]*</(?:applet|script)[^>]*>)"
			+ "|((<[a-z]+)[^>]+script:[^>]+>)"
			+ "|((<[a-z]+)[^>]*(?:on|un)[a-z]+[\\s]*=[\\s]*"
			+ "(?:"
			+ "\"[^\"]*(?:eval|\\.write|cookie|HTML|print|Element)[^\"]*\""
			+ "|'[^']*(?:eval|\\.write|cookie|HTML|print|Element)[^']*'"
			+ "|[^\"'\\s>]*(?:eval|\\.write|cookie|HTML|print|Element)[^\"'\\s>]*"
			+ ")[\\s]*[^>]*>"
			+ ")"
			+ "|(<embed[^>]*>)"
			+ "|(<object[^<>]*(?:>)?)"
			+ "|(<iframe[^>]*>[^<>]*(?:</iframe>)?)";
		
		Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(input);
		StringBuffer sb = new StringBuffer();
		StringBuffer sc;
		
		while (matcher.find()) {
			sc = new StringBuffer();
			
			// object택 처리 - 1. x-scriptlet막기 2. flash에서 외부연동 안되게 막기
			if (matcher.group(8) != null) {
				sc.append(matcher.group(8).replaceAll("(?i:type[\\s]*=[\"'\\s]*text/x-scriptlet[\"']*)", "type=\"text/text\""));
				sc.append("<PARAM NAME=\"AllowScriptAccess\" VALUE=\"never\">");
				matcher.appendReplacement(sb, sc.toString());
			} else if (matcher.group(7) != null) {	// flash에서 외부연동 안되게 막기 - embed택
				sc.append("<EMBED AllowScriptAccess=\"never\" ");
				sc.append(matcher.group(7).substring(7));
				matcher.appendReplacement(sb, sc.toString());
			} else if ((matcher.group(3) != null && matcher.group(4) != null) || (matcher.group(5) != null && matcher.group(6) != null)) {	// 스크립트가 html택 내부에 존재하는 경우
				sc.append("<!-- badtag filtered -->");
				sc.append((matcher.group(4) != null) ? matcher.group(4) : matcher.group(6));
				sc.append(">");
				matcher.appendReplacement(sb, sc.toString());
			} else if (matcher.group(2) != null) {	// script, applet 택
				matcher.appendReplacement(sb, " :script -->");
			} else if (matcher.group(1) != null) {
				matcher.appendReplacement(sb, (matcher.group(1).charAt(1) != '/') ? "<!-- script " : " :script -->");
			} else {	// iframe택
				matcher.appendReplacement(sb, "<!-- % iframe % -->");
			}
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	*/
}
